/*Replicates Figure 5: Adjustment to changes in military spending
Based on replication files to Miyamoto/Nguyen/Sheremirov (2019): "The Effects of Government Spending on Real Exchange Rates: Evidence from Military Spending Panel Data",
Journal of International Economics, Volume 116, 144-157 

may require 
ssc install parmest
ssc install outreg2
*/

clear all
set more off

use MNS-JIE-ReplicationDataset.dta, replace

loc tit1 "Government spending"
loc ytit1 "Percent of GDP"
loc ylab1 "-1(1)3"
loc ymti1 "-1(0.5)3"
loc ysc1 "-1,3"
loc file1 "G"

loc tit2 "Output"
loc ytit2 "Percent"
loc ylab2 "-3(1)3"
loc ymti2 "-3(1)3"
loc ysc2 "-3,3"
loc leg2 "off"
loc file2 "GDP"

loc tit3 "Real exchange rate"
loc ytit3 "Percent"
loc ylab3 "-3(1)3"
loc ymti3 "-3(1)3"
loc ysc3 "-3,3"
loc leg3 "off"
loc file3 "REER"

loc leg `"cols(1) region(lw(none) color(none)) ring(0) position(11) size(vlarge) lab(1 "Point estimate")  lab(3 "90% confidence bands") order(1 3 ) "'

set level 90

tempfile t0 t1 t2 t3

loc i = 1

* Y, REER
xtreg g0_gspnd g0_milspnd_nc l.g0_gspnd warall l.g0_rgdp i.year, fe vce(cl cid)
predict g0_gspnd_iv
gen g0_gspnd_iv_pos = g0_milspnd_nc
replace g0_gspnd_iv_pos = 0 if g0_milspnd_nc<=0
gen g0_gspnd_iv_neg = g0_milspnd_nc
replace g0_gspnd_iv_neg = 0 if g0_milspnd_nc>0


forval h = 0/3 {
	gen g`h'_reer_spliced_paper_sign=-1*g`h'_reer_spliced //account for definition of FX, which is the opposite of the one in the paper
}

gen g0_gspnd_iv_neg_flipped_sign=-1*g0_gspnd_iv_neg //flip sign of negative shock to get correct graph, i.e. response to a 1 unit negative shock

foreach var in rgdp reer_spliced_paper_sign {

	loc ++i
 
	preserve
	forval h = 0/3 {
				parmby "xtreg g`h'_`var' g0_gspnd_iv_pos g0_gspnd_iv_neg_flipped_sign warall l.g0_gspnd l.g0_rgdp l.g0_reer_spliced l.policy_rate i.cid#c.year i.year if kspeg==1,  fe vce(cl cid)", saving("t`var'`h'", replace) ///

			outreg2 using `var', excel append
	}
	* create dataset from estimates
	drop _all
	append using "t`var'0" "t`var'1" "t`var'2" "t`var'3" 
	keep if parm=="g0_gspnd_iv_pos"
		
	gen idnum = _n - 1
		
	* plot
	tw scatter estimate idnum if idnum<3, ///
		msymbol(D) msize(large) mcolor(black)  || ///
	line estimate  min90 max90 idnum if idnum<3, ///
		lc(black black black) lp(solid shortdash shortdash) lwidth(1.25 1 1) ///
		title("`tit`i'' after G hike", size(medlarge)) name(`file`i''_pos, replace) ///
		xtitle("Years after shock", size(medlarge)) xlabel(0(1)2, labsize(medlarge) tp(i)) ///
		ytitle("`ytit`i''", size(medlarge))  ylabel(`ylab`i'',labsize(medlarge) angle(h) grid tp(i)) ymti(`ymti`i'', tp(i) grid) ysc(r(`ysc`i'')) /// 
		scheme(s1color) plotregion(sty(none) margin(zero)) graphregion(margin(small)) ///	 	
		legend(off) ///
		xscale(r(0(1)2)) ///
	    yline(0, lc(black) lw(medthick))  
	
		drop _all
	append using "t`var'0" "t`var'1" "t`var'2" "t`var'3"

	keep if parm=="g0_gspnd_iv_neg_flipped_sign"
	gen idnum = _n - 1
		
	* plot
	tw scatter estimate idnum if idnum<3, ///
		msymbol(D) msize(large) mcolor(black)  || ///
	line estimate  min90 max90 idnum if idnum<3, ///
		lc(black black black) lp(solid shortdash shortdash) lwidth(1.25 1 1) ///
		title("`tit`i'' after G cut" , size(medlarge)) name(`file`i''_neg, replace) ///
		xtitle("Years after shock", size(medlarge)) xlabel(0(1)2, labsize(medlarge) tp(i)) ///
		ytitle("`ytit`i''", size(medlarge))  ylabel(`ylab`i'',labsize(medlarge) angle(h) grid tp(i)) ymti(`ymti`i'', tp(i) grid) ysc(r(`ysc`i'')) /// 
		scheme(s1color) plotregion(sty(none) margin(zero)) graphregion(margin(small)) ///	 	
		legend(off) ///
		xscale(r(0(1)2)) ///
	    yline(0, lc(black) lw(medthick)) 
	
	

	restore
	}
	gr combine GDP_neg GDP_pos REER_neg REER_pos, ///
	plotregion(sty(none) margin(zero))  scheme(s1color) rows(2) name(`graph`i'')
	graph display, ysize(6) xsize(6)	
			 
	graph export "Figure_`file`i''_mil_peg.pdf", as(pdf) replace fontface("Georgia")